package dataMapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;

import dataEntity.Demographics;

public class DemographicsMapper {
	public static Map<String,Map<String,Demographics>> process(BufferedReader br){
		Map<String,Map<String,Demographics>> map = new HashMap<String,Map<String,Demographics>> ();
		try {
			br.readLine();
			while(br.ready()){
				Iterable<CSVRecord> parser = CSVFormat.EXCEL.parse(br);
				for (CSVRecord record : parser) {
					Demographics object = buildObject(record);

					Map<String,Demographics> stateMap = map.get(object.getCHSI_State_Abbr());
					if(stateMap == null) stateMap = new HashMap<String,Demographics>();
					stateMap.put(object.getCHSI_County_Name(), object);
					map.put(object.getCHSI_State_Abbr(), stateMap);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}
	
	public static void info(Map<String,Map<String,Demographics>> map){
		TreeSet<Integer> infoSet = new TreeSet<Integer>();
		Set<String> stateSet = map.keySet();
		for(String s : stateSet){
			Map<String, Demographics> countyMap = map.get(s);
			Set<String> countySet = countyMap.keySet();
			for(String countyString : countySet){
				Demographics dem = countyMap.get(countyString);
				infoSet.add(dem.getPopulation_Size());
			}
		}
//		System.out.println("Population Smallest " + infoSet.first() + " Largest " + infoSet.last());
	}
	
	public static  Demographics buildObject(CSVRecord record){
		Demographics object = new Demographics();
		object.setState_FIPS_Code(record.get(0));
		object.setCounty_FIPS_Code(record.get(1));
		object.setCHSI_County_Name(record.get(2));
		object.setCHSI_State_Name(record.get(3));
		object.setCHSI_State_Abbr(record.get(4));
		object.setStrata_ID_Number(record.get(5));
		object.setStrata_Determining_Factors(record.get(6));
		object.setNumber_Counties(record.get(7));
		object.setPopulation_Size(record.get(8));
		object.setMin_Population_Size(record.get(9));
		object.setMax_Population_Size(record.get(10));
		object.setPopulation_Density(record.get(11));
		object.setMin_Population_Density(record.get(12));
		object.setMax_Population_Density(record.get(13));
		object.setPoverty(record.get(14));
		object.setMin_Poverty(record.get(15));
		object.setMax_Poverty(record.get(16));
		object.setAge_19_Under(record.get(17));
		object.setMin_Age_19_Under(record.get(18));
		object.setMax_Age_19_Under(record.get(19));
		object.setAge_19_64(record.get(20));
		object.setMin_Age_19_64(record.get(21));
		object.setMax_Age_19_65(record.get(22));
		object.setAge_65_84(record.get(23));
		object.setMin_Age_65_84(record.get(24));
		object.setMax_Age_65_85(record.get(25));
		object.setAge_85_and_Over(record.get(26));
		object.setMin_Age_85_and_Over(record.get(27));
		object.setMax_Age_85_and_Over(record.get(28));
		object.setWhite(record.get(29));
		object.setMin_White(record.get(30));
		object.setMax_White(record.get(31));
		object.setBlack(record.get(32));
		object.setMin_Black(record.get(33));
		object.setMax_Black(record.get(34));
		object.setNative_American(record.get(35));
		object.setMin_Native_American(record.get(36));
		object.setMax_Native_American(record.get(37));
		object.setAsian(record.get(38));
		object.setMin_Asian(record.get(39));
		object.setMax_Asian(record.get(40));
		object.setHispanic(record.get(41));
		object.setMin_Hispanic(record.get(42));
		object.setMax_Hispanic(record.get(43));
		return object;
	}

}
